# !/usr/bin/env python3
# -*-coding:utf-8 -*-

"""
# Time       ：2021/10/19 14:21
# Author     ：bai
# version    ：python 3.8
"""
import re

from lxml import etree


def parse(page_text):
    page_html = etree.HTML(page_text)
    description, keywords = '', ''

    try:
        # step 1 判断标签name
        description = page_html.xpath('//meta[@name="description"]/@content')
        keywords = page_html.xpath('//meta[@name="keywords"]/@content')
        if description:
            description = description[0].strip()
            keywords = keywords[0].strip() if keywords else ''
            return description, keywords

        # step 2 判断标签property
        description = page_html.xpath('//meta[@property="og:description"]/@content')
        keywords = page_html.xpath('//meta[@property="og:image"]/@content')
        if description:
            description = description[0].strip()
            keywords = keywords[0].strip() if keywords else ''
            return description, keywords

        # step 3 re 匹配头部说明
        description = re.findall(r'<meta.*?description" content="(.*?)">', page_text)
        description = description[0] if description else ''
        keywords = re.findall(r'<meta.*?keywords" content="(.*?)">', page_text)
        keywords = keywords[0] if keywords else ''
        if description:
            return description, keywords

        # step4 判断div标签
        # print('step4')
        d = page_html.xpath('//div[contains(@class, "description")]//text()')
        k = page_html.xpath('//div[contains(@class, "keyword")]//text()')
        if d:
            description = ''.join(d).strip()
        if k:
            keywords = ''.join(k).strip()
        if description:
            return description, keywords

    except Exception as e:
        print(e)
        pass
    return description, keywords


if __name__ == '__main__':
    with open('test3.html', 'r', encoding='utf-8') as f:
        h = f.read()
        print(parse(h))
